package com.yzcs.tvapp.simple.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.cxy.common.request.ApiAddress
import com.cxy.common.request.NetDialog
import com.fy.baselibrary.application.mvvm.IBaseMVVM
import com.fy.baselibrary.rv.divider.GridItemDecoration
import com.fy.baselibrary.utils.AnimUtils
import com.fy.baselibrary.utils.JumpUtils
import com.fy.baselibrary.utils.notify.T
import com.fy.baselibrary.widget.refresh.EasyPullLayout
import com.yzcs.tvapp.R
import com.yzcs.tvapp.databinding.RecommendVideoListLayoutBinding
import com.yzcs.tvapp.simple.fragment.SimpleFragmentActivity
import java.util.*

/**
 * description 豆瓣 推荐 视频 列表
 * Created by fangs on 2021/9/16 11:34.
 */
class RecommendVideoActivity : AppCompatActivity(),
    IBaseMVVM<RecommendVideoViewModel, RecommendVideoListLayoutBinding> {

    lateinit var vdb: RecommendVideoListLayoutBinding
    lateinit var rvm: RecommendVideoViewModel

    lateinit var adapter: VideoListAdapter
    private var itemLength = 3

    override fun setContentLayout(): Int {
        return R.layout.recommend_video_list_layout
    }

    override fun initData(
        viewModel: RecommendVideoViewModel?,
        dataBinding: RecommendVideoListLayoutBinding?,
        savedInstanceState: Bundle?
    ) {
        vdb = dataBinding!!
        rvm = viewModel!!

        AnimUtils.setToolbar(this, vdb.layoutTitle.toolbar)

        initRv()
    }

    private fun setLayoutManager(): GridLayoutManager{
        val layoutManager = GridLayoutManager(this, itemLength)
        layoutManager.orientation = GridLayoutManager.VERTICAL

        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){
            override fun getSpanSize(position: Int): Int {
                val videoBean = adapter.getmDatas()[position]
                return if (videoBean.type == ApiAddress.listTitleType) itemLength else 1
            }
        }

        return layoutManager
    }

    private fun initRv(){
        vdb.rvRecommendVideo.layoutManager = setLayoutManager()

        adapter = VideoListAdapter(this, ArrayList())
        vdb.rvRecommendVideo.adapter = adapter

        adapter.setItemClickListener { view ->
            val videoBean = view.tag as VideoBean
            val bundle = Bundle()
            bundle.putSerializable("videoBean", videoBean)
            JumpUtils.jump(this, SimpleFragmentActivity::class.java, bundle)
        }

        vdb.rvRecommendVideo.addItemDecoration(GridItemDecoration.Builder.init().create(this))

        vdb.epl.setOnRefreshListener {
            rvm.getRecommendVideoData(this, NetDialog().init(this, vdb.epl))
                .observe(this,
                    Observer<List<VideoBean>> { data ->
                        adapter.setmDatas(data)
                        adapter.notifyDataSetChanged()
                    })
        }
        vdb.epl.start(EasyPullLayout.TYPE_EDGE_TOP)
    }

}